package com.mall.server.controller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.ResourceCategoryService;
import com.tanhua.model.domain.UmsResourceCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/resourceCategory")
public class ResourceCategoryController {

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    /**
     * 查询所有后台资源分类
     *
     * @return
     */
    @GetMapping("/listAll")
    public R listAll() {

        List<UmsResourceCategory> umsResource = resourceCategoryService.listAll();

        if (umsResource == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return R.success(umsResource);
    }

    /**
     * 根据ID删除后台资源
     *
     * @return
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        resourceCategoryService.deletes(id);
        return R.success("操作成功");
    }

    /**
     * 添加后台资源分类
     * @param umsResourceCategory
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody UmsResourceCategory umsResourceCategory) {
        resourceCategoryService.create(umsResourceCategory);
        return R.success("操作成功");
    }

    /**
     * 修改后台资源分类
     * @param umsResourceCategory
     * @return
     */
    @PostMapping("/update/{id}")
    public R updates(@RequestBody UmsResourceCategory umsResourceCategory,@PathVariable Integer id) {
        resourceCategoryService.updates(umsResourceCategory,id);
        return R.success("操作成功");
    }
}
